package cn.szzsi._webpc;

import cn.szzsi._web.BaseWebController;
import cn.szzsi.common.bean.CartItemCookie;
import cn.szzsi.common.bean.SystemConfig;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.SystemConfigUtil;
import cn.szzsi.common.model.CartItem;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.interceptor.CommonInterceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 前台类 - 购物车项
 * 
 */
public class CartItemController extends BaseWebPcController<CartItem> {

	private Product product;
	private Integer quantity;// 商品数量
	private Integer totalQuantity;// 商品总数
	private Integer totalPoint;// 总积分
	private BigDecimal totalPrice;// 总计商品价格
	private String path;//模板路径
	private String uid;//商户id,也是用户的店铺id
	private String shopId;//店铺id
	private List<CartItem> cartItemList = new ArrayList<CartItem>();
	
	// 购物车项列表
	@Clear
	@Before(CommonInterceptor.class)
	public void list() {
		Member loginMember = getLoginMember();
		totalQuantity = 0;
		totalPoint = 0;
		totalPrice = new BigDecimal("0");
		shopId=getPara("shopId");
		SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
		//String shopAlias=siteConfig.getStr("shopAlias");
		if (loginMember == null) {
			Cookie[] cookies = getRequest().getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (StringUtils.equalsIgnoreCase(cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) {
						if (StringUtils.isNotEmpty(cookie.getValue())) {
							JSONArray jsonArray = null;
							try {
								jsonArray = JSON.parseArray(URLDecoder.decode(cookie.getValue(),"UTF-8"));
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							List<CartItemCookie> cartItemCookieList = JSON.parseArray(jsonArray.toJSONString(), CartItemCookie.class);
							for (CartItemCookie cartItemCookie : cartItemCookieList) {
								Product product = Product.dao.findById(cartItemCookie.getI());
								if (product != null) {
									totalQuantity += cartItemCookie.getQ();
									if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
										totalPoint = product.getInt("point") * cartItemCookie.getQ() + totalPoint;
									}
									totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItemCookie.getQ().toString())).add(totalPrice);
									CartItem cartItem = new CartItem();
									cartItem.set("product_id",product.getStr("id"));
									cartItem.set("quantity",cartItemCookie.getQ());
									cartItemList.add(cartItem);
								}
							}
						}
					}
				}
			}
		} else {
			cartItemList = loginMember.getCartItemList();
			if (cartItemList != null) {
				for (CartItem cartItem : cartItemList) {
					Product product=cartItem.getProduct();
					totalQuantity += cartItem.getInt("quantity");
					if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
						totalPoint = product.getInt("point") * cartItem.getInt("quantity") + totalPoint;
					}
					totalPrice = product.getPrice().multiply(new BigDecimal(cartItem.getInt("quantity").toString())).add(totalPrice);
					totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(totalQuantity)) ;//会员打折后的价格
				}
			}
		}
		totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
		if (getSystemConfig().getPointType() == SystemConfig.PointType.orderAmount) {
			totalPoint = totalPrice.multiply(new BigDecimal(getSystemConfig().getPointScale().toString())).setScale(0, RoundingMode.DOWN).intValue();
		}
		setAttr("totalPoint", totalPoint);
		setAttr("totalPrice", totalPrice);
		setAttr("totalQuantity", totalQuantity);
		setAttr("cartItemList", cartItemList);
		pcTemplatePath("cart_item_list.html");
	}
	
	// 添加购物车项
	@Clear
	public void ajaxAdd() {
		String id = getPara("id","");
		String specificationValueIds = getPara("specificationValueIds");
		if (StringUtils.isNotBlank(specificationValueIds)) {
			Product tmp = Product.dao.findById(id);
			String specificationType = tmp.getSpecificationtype();//获取商品系列
			product = Product.getProductBySpecificationvalues(specificationValueIds, specificationType);
			if (product != null) {
				id = product.getId();
			}
		}else {
			if(StrKit.isBlank(id)){
				addActionError("产品ID为空!");
				return;
			}else {
				product = Product.dao.findById(id);
			}
		}
		quantity = getParaToInt("quantity",0);
		if (product == null && !product.getBoolean("isMarketable")) {
			ajaxJsonErrorMessage("此商品已下架!");
			return;
		}
		if (quantity == null || quantity < 1) {
			quantity = 1;
		}
		Integer totalQuantity = 0;// 总计商品数量
		BigDecimal totalPrice = new BigDecimal("0");// 总计商品价格
		Member loginMember = getLoginMember();
		if (loginMember == null) {
			List<CartItemCookie> cartItemCookieList = new ArrayList<CartItemCookie>();
			boolean isExist = false;
			Cookie[] cookies = getRequest().getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (StringUtils.equalsIgnoreCase(cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) {
						if (StringUtils.isNotEmpty(cookie.getValue())) {
							JSONArray jsonArray = null;
							try {
								jsonArray = JSON.parseArray(URLDecoder.decode(cookie.getValue(),"UTF-8"));
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							List<CartItemCookie> previousCartItemCookieList = JSON.parseArray(jsonArray.toJSONString(), CartItemCookie.class);
							
							for (CartItemCookie previousCartItemCookie : previousCartItemCookieList) {
								Product cartItemCookieProduct = Product.dao.findById(previousCartItemCookie.getI());
								if (StringUtils.equals(previousCartItemCookie.getI(), id)) {
									isExist = true;
									previousCartItemCookie.setQ(previousCartItemCookie.getQ() + quantity);
									if (product.getInt("store") != null && (product.getInt("freezeStore") + previousCartItemCookie.getQ()) > product.getInt("store")) {
										ajaxJsonErrorMessage("添加购物车失败，商品库存不足!");
									}
								}
								cartItemCookieList.add(previousCartItemCookie);
								totalQuantity += previousCartItemCookie.getQ();
								totalPrice =  cartItemCookieProduct.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(previousCartItemCookie.getQ().toString())).add(totalPrice);
							}
						}
					}
				}
			}
			if (!isExist) {
				CartItemCookie cartItemCookie = new CartItemCookie();
				cartItemCookie.setI(id);
				cartItemCookie.setQ(quantity);
				cartItemCookieList.add(cartItemCookie);
				totalQuantity += quantity;
				totalPrice =  product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(quantity.toString())).add(totalPrice);
				if (product.getInt("store") != null && (product.getInt("freezeStore") + cartItemCookie.getQ()) > product.getInt("store")) {
					ajaxJsonErrorMessage("添加购物车失败，商品库存不足!");
				}
			}
			for (CartItemCookie cartItemCookie : cartItemCookieList) {
				if (StringUtils.equals(cartItemCookie.getI(), id)) {
					Product cartItemCookieProduct = Product.dao.findById(cartItemCookie.getI());
					if (product.getInt("store") != null && (cartItemCookieProduct.getInt("freezeStore") + cartItemCookie.getQ()) > cartItemCookieProduct.getInt("store")) {
						ajaxJsonErrorMessage("添加购物车失败，商品库存不足!");
					}
				}
			}
			//保存未登录会员购物车项集合的Cookie名称
			String jsonText= null;
			try {
				jsonText = URLEncoder.encode(JsonKit.toJson(cartItemCookieList),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			Cookie cookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonText);
			cookie.setPath(getRequest().getContextPath() + "/");
			cookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
			setCookie(cookie);

		} else {
			boolean isExist = false;
			List<CartItem> previousCartItemList = loginMember.getCartItemList();
			if (previousCartItemList != null) {
				for (CartItem previousCartItem : previousCartItemList) {
					if (StringUtils.equals(previousCartItem.getProduct().getStr("id"), id)) {
						isExist = true;
						previousCartItem.set("quantity",previousCartItem.getInt("quantity") + quantity);
						if (product.getInt("store") != null && (product.getInt("freezeStore") + previousCartItem.getInt("quantity")) > product.getInt("store")) {
							ajaxJsonErrorMessage("添加购物车失败，商品库存不足!");
						}
						previousCartItem.update();
					}
					totalQuantity += previousCartItem.getInt("quantity");
					totalPrice =  previousCartItem.getProduct().getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(previousCartItem.getInt("quantity").toString())).add(totalPrice);
				}
			}
			if (!isExist) {
				CartItem cartItem = new CartItem();
				cartItem.set("id", CommonUtil.getUUID());
				cartItem.set("createDate", new Date());
				cartItem.set("member_id",loginMember.getStr("id"));
				cartItem.set("product_id",product.getStr("id"));
				cartItem.set("quantity",quantity);
				if (product.getInt("store") != null && (product.getInt("freezeStore") + cartItem.getInt("quantity")) > product.getInt("store")) {
					ajaxJsonErrorMessage("添加购物车失败，商品库存不足!");
				}
				cartItem.save();
				totalQuantity += quantity;
				totalPrice =  product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(quantity.toString())).add(totalPrice);
			}
		}
		//将购物车总数量添加至cookie
		Cookie quantity = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_TOTAL_QUANTITY, totalQuantity.toString());
		quantity.setPath(getRequest().getContextPath() + "/");
		quantity.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
		getResponse().addCookie(quantity);

		totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
		DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat());
		String totalPriceString = decimalFormat.format(totalPrice);
		//购物车

		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put(MESSAGE, "添加至购物车成功！");
		jsonMap.put("totalQuantity", totalQuantity.toString());
		jsonMap.put("totalPrice", totalPriceString);


		if(StrKit.notBlank(getPara("returnUrl"))){
			jsonMap.put("returnUrl", getPara("returnUrl"));
		}
		renderJson(jsonMap);
	}
		
	// 购物车项列表
	@Clear
	public void ajaxList() {
		List<Map<String, String>> jsonList = new ArrayList<Map<String, String>>();
		Member loginMember = getLoginMember();
		totalQuantity = 0;
		totalPrice = new BigDecimal("0");
		if (loginMember == null) {
			Cookie[] cookies = getRequest().getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (StringUtils.equalsIgnoreCase(cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) {
						if (StringUtils.isNotEmpty(cookie.getValue())) {

							JSONArray jsonArray = null;
							try {
								jsonArray = JSON.parseArray(URLDecoder.decode(cookie.getValue(),"UTF-8"));
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							List<CartItemCookie> cartItemCookieList = JSON.parseArray(jsonArray.toJSONString(), CartItemCookie.class);
							for (CartItemCookie cartItemCookie : cartItemCookieList) {
								Product product = Product.dao.findById(cartItemCookie.getI());
								if (product != null) {
									totalQuantity += cartItemCookie.getQ();
									totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItemCookie.getQ().toString())).add(totalPrice);
									DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat());
									String priceString = decimalFormat.format(product.getPreferentialPrice(getLoginMember()));
									Map<String, String> jsonMap = new HashMap<String, String>();
									jsonMap.put("name", product.getStr("name"));
									jsonMap.put("price", priceString);
									jsonMap.put("quantity", cartItemCookie.getQ().toString());
									jsonMap.put("htmlFilePath", product.getStr("htmlFilePath"));
									jsonList.add(jsonMap);
								}
							}
						}
					}
				}
			}
		} else {
			System.out.println(loginMember);
			List<CartItem> cartItemSet = loginMember.getCartItemList();
			if (cartItemSet != null) {
				for (CartItem cartItem : cartItemSet) {
					Product product = cartItem.getProduct();
					totalQuantity += cartItem.getInt("quantity");
					totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItem.getInt("quantity").toString())).add(totalPrice);
					DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat());
					String priceString = decimalFormat.format(cartItem.getProduct().getPreferentialPrice(getLoginMember()));
					Map<String, String> jsonMap = new HashMap<String, String>();
					jsonMap.put("name", product.getStr("name"));
					jsonMap.put("price", priceString);
					jsonMap.put("quantity", cartItem.getInt("quantity").toString());
					jsonMap.put("htmlFilePath", cartItem.getProduct().getStr("htmlFilePath"));
					jsonList.add(jsonMap);
				}
			}
		}
		totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
		DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat());
		String totalPriceString = decimalFormat.format(totalPrice);
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put("totalQuantity", totalQuantity.toString());
		jsonMap.put("totalPrice", totalPriceString);
		jsonList.add(0, jsonMap);
		renderJson(jsonList);
	}
	
	// 修改购物车项
	@Clear
	public void ajaxEdit() {
		quantity = getParaToInt("quantity",0);
		String id = getPara("id");
		if (quantity == null || quantity < 1) {
			quantity = 1;
		}
		Member loginMember = getLoginMember();
		totalQuantity = 0;
		totalPoint = 0;
		totalPrice = new BigDecimal("0");
		BigDecimal subtotalPrice = new BigDecimal("0");
		if (loginMember == null) {
			Cookie[] cookies = getRequest().getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (StringUtils.equalsIgnoreCase(cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) {
						if (StringUtils.isNotEmpty(cookie.getValue())) {
							JSONArray jsonArray = null;
							try {
								jsonArray = JSON.parseArray(URLDecoder.decode(cookie.getValue(),"UTF-8"));
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							List<CartItemCookie> cartItemCookieList = JSON.parseArray(jsonArray.toJSONString(), CartItemCookie.class);
							Iterator<CartItemCookie> iterator = cartItemCookieList.iterator();
							while (iterator.hasNext()) {
								CartItemCookie cartItemCookie = iterator.next();
								Product product = Product.dao.findById(cartItemCookie.getI());
								if (StringUtils.equals(id, cartItemCookie.getI())) {
									cartItemCookie.setQ(quantity);
									subtotalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(quantity));
									if (product.getInt("store") != null && (product.getInt("freezeStore") + cartItemCookie.getQ()) > product.getInt("store")) {
										ajaxJsonErrorMessage("商品库存不足！");
									}
								}
								totalQuantity += cartItemCookie.getQ();
								if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
									totalPoint = product.getInt("point") * cartItemCookie.getQ() + totalPoint;
								}
								totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItemCookie.getQ().toString())).add(totalPrice);
							}
							//String jsonText = JSON.toJSONString(cartItemCookieList,true);
							String jsonText= null;
							try {
								jsonText = URLEncoder.encode(JsonKit.toJson(cartItemCookieList),"UTF-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							Cookie newCookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonText);
							newCookie.setPath(getRequest().getContextPath() + "/");
							newCookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
							getResponse().addCookie(newCookie);
						}
					}
				}
			}
		} else {
			cartItemList = loginMember.getCartItemList();
			if (cartItemList != null) {
				for (CartItem cartItem : cartItemList) {
					Product product = cartItem.getProduct();
					if (StringUtils.equals(id, cartItem.getProduct().getStr("id"))) {
						cartItem.set("quantity",quantity);
						if (product.getInt("store") != null && (product.getInt("freezeStore") + cartItem.getInt("quantity")) > product.getInt("store")) {
							ajaxJsonErrorMessage("商品库存不足！");
						}
						cartItem.update();
						subtotalPrice = cartItem.getSubtotalPrice();
					}
					totalQuantity += cartItem.getInt("quantity");
					if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
						totalPoint = product.getInt("point") * cartItem.getInt("quantity") + totalPoint;
					}
					totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItem.getInt("quantity").toString())).add(totalPrice);
				}
			}
		}
		DecimalFormat orderUnitCurrencyFormat = new DecimalFormat(getOrderUnitCurrencyFormat());
		DecimalFormat orderCurrencyFormat = new DecimalFormat(getOrderCurrencyFormat());
		totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
		if (getSystemConfig().getPointType() == SystemConfig.PointType.orderAmount) {
			totalPoint = totalPrice.multiply(new BigDecimal(getSystemConfig().getPointScale().toString())).setScale(0, RoundingMode.DOWN).intValue();
		}

		//将购物车总数量添加至cookie
		Cookie quantity = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_TOTAL_QUANTITY, totalQuantity.toString());
		quantity.setPath(getRequest().getContextPath() + "/");
		quantity.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
		getResponse().addCookie(quantity);

		String subtotalPriceString = orderCurrencyFormat.format(subtotalPrice);
		String totalPriceString = orderUnitCurrencyFormat.format(totalPrice);
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put("subtotalPrice", subtotalPriceString);
		jsonMap.put("totalQuantity", totalQuantity.toString());
		jsonMap.put("totalPoint", totalPoint.toString());
		jsonMap.put("totalPrice", totalPriceString);
		jsonMap.put(STATUS, SUCCESS);
		renderJson(jsonMap);
	}
	
	// 删除购物车项
	@Clear
	public void ajaxDelete() {
		id = getPara("id","");
		Member loginMember = getLoginMember();
		totalQuantity = 0;
		totalPoint = 0;
		totalPrice = new BigDecimal("0");
		if (loginMember == null) {
			Cookie[] cookies = getRequest().getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (StringUtils.equalsIgnoreCase(cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) {
						if (StringUtils.isNotEmpty(cookie.getValue())) {
							JSONArray previousJsonArray = null;
							try {
								previousJsonArray = JSON.parseArray(URLDecoder.decode(cookie.getValue(),"UTF-8"));
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							List<CartItemCookie> cartItemCookieList = JSON.parseArray(previousJsonArray.toJSONString(), CartItemCookie.class);
							Iterator<CartItemCookie> iterator = cartItemCookieList.iterator();
							while (iterator.hasNext()) {
								CartItemCookie cartItemCookie = iterator.next();
								if (StringUtils.equals(cartItemCookie.getI(), id)) {
									iterator.remove();
								} else {
									Product product = Product.dao.findById(cartItemCookie.getI());
									totalQuantity += cartItemCookie.getQ();
									if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
										totalPoint = product.getInt("point") * cartItemCookie.getQ() + totalPoint;
									}
									totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItemCookie.getQ().toString())).add(totalPrice);
								}
							}

							String jsonText= null;
							try {
								jsonText = URLEncoder.encode(JsonKit.toJson(cartItemCookieList),"UTF-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							Cookie newCookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonText);
							newCookie.setPath(getRequest().getContextPath() + "/");
							newCookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
							getResponse().addCookie(newCookie);
						}
					}
				}
			}
		} else {
			System.out.println("用户已登录---------------");
			List<CartItem> cartItemList = loginMember.getCartItemList();
			if (cartItemList != null) {
				for (CartItem cartItem : cartItemList) {
					if (StringUtils.equals(cartItem.getProduct().getStr("id"), id)) {
						cartItem.delete();
					} else {
						Product product = cartItem.getProduct();
						totalQuantity += cartItem.getInt("quantity");
						if (getSystemConfig().getPointType() == SystemConfig.PointType.productSet) {
							totalPoint = product.getInt("point") * cartItem.getInt("quantity") + totalPoint;
						}
						totalPrice = product.getPreferentialPrice(getLoginMember()).multiply(new BigDecimal(cartItem.getInt("quantity").toString())).add(totalPrice);
					}
				}
			}
		}
		totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
		if (getSystemConfig().getPointType() == SystemConfig.PointType.orderAmount) {
			totalPoint = totalPrice.multiply(new BigDecimal(getSystemConfig().getPointScale().toString())).setScale(0, RoundingMode.DOWN).intValue();
		}

		//将购物车总数量添加至cookie
		Cookie quantity = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_TOTAL_QUANTITY, totalQuantity.toString());
		quantity.setPath(getRequest().getContextPath() + "/");
		quantity.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE);
		getResponse().addCookie(quantity);

		DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat());
		String totalPriceString = decimalFormat.format(totalPrice);
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put("totalQuantity", totalQuantity.toString());
		jsonMap.put("totalPoint", totalPoint.toString());
		jsonMap.put("totalPrice", totalPriceString);
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put(MESSAGE, "商品删除成功！");
		renderJson(jsonMap);
	}
	
	// 清空购物车项
	@Clear
	public void ajaxClear() {
		Member loginMember = getLoginMember();
		if (loginMember == null) {
			Cookie cookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, null);
			cookie.setPath(getRequest().getContextPath() + "/");
			cookie.setMaxAge(0);
			getResponse().addCookie(cookie);
		} else {
			List<CartItem> cartItemSet = loginMember.getCartItemList();
			if (cartItemSet != null) {
				for (CartItem cartItem : cartItemSet) {
					cartItem.delete();
				}
			}
		}
		ajaxJsonSuccessMessage("购物车清空成功！");
	}
}
